<template>
  <div class="app-container">
    <!-- 把所有字段都生成了，先删除不要的字段，再做定制化 -->
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="服务器IP" prop="serverIpLike">
            <el-input
              v-model="queryParams.serverIpLike" placeholder="请输入服务器IP" clearable size="default" style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="服务器端口" prop="serverPort">
            <el-input v-model="queryParams.serverPort" placeholder="请输入服务器端口" clearable size="default" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col> -->
        <el-col :span="6">
          <el-form-item label="数据库用户名" prop="jdbcUsernameLike">
            <el-input
              v-model="queryParams.jdbcUsernameLike" placeholder="请输入数据库用户名" clearable size="default" style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="数据库密码" prop="jdbcPasswordLike">
            <el-input v-model="queryParams.jdbcPasswordLike" placeholder="请输入数据库密码" clearable size="default" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col> -->
        <!-- </el-row>
      <el-row> -->
        <el-col :span="6">
          <el-form-item label="是否启用" prop="isEnable">
            <el-radio-group v-model="queryParams.isEnable">
              <el-radio key="" label="">全部</el-radio>
              <el-radio v-for="dict in isEnableOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="创建人" prop="createByLike">
            <el-input v-model="queryParams.createByLike" placeholder="请输入创建人" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="创建时间" prop="createTime">
            <el-input v-model="queryParams.createTime" placeholder="请输入创建时间" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="更新人" prop="updateByLike">
            <el-input v-model="queryParams.updateByLike" placeholder="请输入更新人" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="更新时间" prop="updateTime">
            <el-input v-model="queryParams.updateTime" placeholder="请输入更新时间" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remarkLike">
            <el-input v-model="queryParams.remarkLike" placeholder="请输入备注" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col> -->
        <el-col :span="6">
          <el-form-item>
            <search-button @click="handleQuery" :perm="['data:admin:server:search']" />
            <reset-button @click="resetQuery" :perm="['data:admin:server:search']" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <add-button @click="handleAdd" :perm="['data:admin:server:insert']" :roleType="['app']" />
      <delete-button @click="handleDelete" :disabled="multiple" :perm="['data:admin:server:delete']" :roleType="['app']" />
      <right-toolbar v-model:showSearch="showSearch" @query-table="getList" :perm="['data:admin:server:search']" />
    </el-row>

    <!-- 把所有字段都生成了，先删除不要的字段，再做定制化 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="服务器ID" prop="serverId" :show-overflow-tooltip="true" min-width="100"/> -->
      <el-table-column label="服务器IP" prop="serverIp" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="服务器端口" prop="serverPort" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="服务器名称" prop="serverName" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="服务器类型" prop="serverType" :show-overflow-tooltip="true" min-width="100">
        <template #default="scope">
          {{ selectDictLabel(serverTypeOptions, scope.row.serverType) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="数据库用户名" prop="jdbcUsername" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="数据库密码" prop="jdbcPassword" :show-overflow-tooltip="true" min-width="100"/> -->
      <el-table-column label="是否启用" prop="isEnable" align="center" min-width="100">
        <template #default="scope">
          <el-tag :type="isEnableTag(scope.row.isEnable)" v-if="scope.row.isEnable != undefined">
            {{ selectDictLabel(isEnableOptions, scope.row.isEnable) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" align="center" width="120" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="160" />
      <!-- <el-table-column label="更新人" prop="updateBy" align="center" width="120"/>
      <el-table-column label="更新时间" prop="updateTime" align="center" width="160"/>
      <el-table-column label="备注" prop="remark" :show-overflow-tooltip="true" min-width="100"/> -->
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width" v-if="hasRoleType(['app'])">
        <template #default="scope">
          <text-edit-button @click="handleUpdate(scope.row)" :perm="['data:admin:server:update']" :roleType="['app']" />
          <text-delete-button
            @click="handleDelete(scope.row)" :perm="['data:admin:server:delete']" :roleType="['app']"
            :title="'是否确认删除记录'+scope.row.serverId+'?'" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 新增或修改对话框 -->
    <el-dialog v-el-drag-dialog :title="editForm.title" :close-on-click-modal="false" v-model="editForm.open" width="800px" append-to-body>
      <el-form ref="formRef" :model="editForm.data" :rules="editForm.rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="服务器IP" prop="serverIp">
              <el-input v-model="editForm.data.serverIp" placeholder="请输入服务器IP" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="服务器端口" prop="serverPort">
              <el-input v-model="editForm.data.serverPort" placeholder="请输入服务器端口" clearable />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="服务器名称" prop="serverName">
              <el-input v-model="editForm.data.serverName" placeholder="请输入服务器名称" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="服务器类型" prop="serverType">
              <el-select v-model="editForm.data.serverType" placeholder="请选择服务器类型" clearable size="small">
                <el-option v-for="dict in serverTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>        
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="数据库用户名" prop="jdbcUsername">
              <el-input v-model="editForm.data.jdbcUsername" placeholder="请输入数据库用户名" type="password" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="数据库密码" prop="jdbcPassword">
              <el-input v-model="editForm.data.jdbcPassword" placeholder="请输入数据库密码" type="password" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="是否启用" prop="isEnable">
              <el-radio-group v-model="editForm.data.isEnable">
                <el-radio v-for="dict in isEnableOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="editForm.data.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <dialog-footer @ok="submitForm" @cancel="cancel" />
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { search, insert, deleteByIds, updateById, selectById } from "@data/api/data/dataserver";
import { debounceWrapper } from "@/utils/zmids";

export default {
  // 此处的名字需要与菜单中Path一致，否则多Tab页切换时会刷新
  name: 'DataServer',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 是否启用的固定字典选项列表
      isEnableOptions: [{"dictValue":"Y","dictLabel":"启用"},{"dictValue":"N","dictLabel":"禁用"}],
      // 服务器类型
      serverTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 服务器ID
        serverId: undefined,
        // 服务器IP
        serverIpLike: '',
        // 服务器端口
        serverPort: undefined,
        // 数据库用户名
        jdbcUsernameLike: '',
        // 数据库密码
        jdbcPasswordLike: '',
        // 是否启用
        isEnable: '',
        // 创建人
        createByLike: '',
        // 创建时间
        createTime: undefined,
        // 更新人
        updateByLike: '',
        // 更新时间
        updateTime: undefined,
        // 备注
        remarkLike: '',
      },
      editForm: {
        // 控制 dialog
        open: false,
        // 弹出层标题
        title: '',
        // 是否修改操作
        isModify: false,
        // 表单参数
        data: {},
        // 规则
        rules: {
          // 服务器IP
          serverIp: [
            { required: true, message: '服务器IP不可以为空', trigger: 'blur' }
          , { min: 1, max: 128, message: "服务器IP的长度最多 128 个字符", trigger: "blur" }
          ],
          // 服务器端口
          serverPort: [
            { required: true, message: '服务器端口不可以为空', trigger: 'blur' }
          ],
          // 服务器名称
          serverName: [
            { required: true, message: '服务器名称不可以为空', trigger: 'blur' }
          , { min: 1, max: 128, message: "服务器名称的长度最多 128 个字符", trigger: "blur" }
          ],
          // 服务器类型
          serverType: [
            { required: true, message: '服务器类型不可以为空', trigger: 'blur' }
          , { min: 1, max: 128, message: "服务器类型的长度最多 1 个字符", trigger: "blur" }
          ],
          // 数据库用户名
          jdbcUsername: [
            { required: true, message: '数据库用户名不可以为空', trigger: 'blur' }
          , { min: 1, max: 128, message: "数据库用户名的长度最多 128 个字符", trigger: "blur" }
          ],
          // 数据库密码
          jdbcPassword: [
            { required: true, message: '数据库密码不可以为空', trigger: 'blur' }
          , { min: 1, max: 256, message: "数据库密码的长度最多 256 个字符", trigger: "blur" }
          ],
          // 是否启用
          isEnable: [
            { required: true, message: '是否启用不可以为空', trigger: 'blur' }
          , { min: 1, max: 1, message: "是否启用的长度最多 1 个字符", trigger: "blur" }
          ],
        },
      }
    };
  },
  created() {
    // 服务器类型
    this.getDicts("server_type").then(response => {
      this.serverTypeOptions = response.data;
    });

    // 查询列表
    this.getList();

  },
  methods: {
    // 查询列表
    getList: debounceWrapper(function () {
      this.loading = true;
      search(this.queryParams).then(
        response => {
          this.dataList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    }),
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.editForm.isModify = false;
      this.editForm.data = {
        // 服务器ID
        serverId: undefined,
        // 服务器IP
        serverIp: '',
        // 服务器端口
        serverPort: undefined,
        // 数据库用户名
        jdbcUsername: '',
        // 数据库密码
        jdbcPassword: '',
        // 是否启用
        isEnable: '',
        // 创建人
        createBy: '',
        // 创建时间
        createTime: undefined,
        // 更新人
        updateBy: '',
        // 更新时间
        updateTime: undefined,
        // 备注
        remark: '',
      };
      // 重置字段校验
      this.$refs.formRef && this.$refs.formRef.clearValidate();
      this.resetForm("formRef");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // 所有选中的ID
      this.ids = selection.map(item => item.serverId)
      // 激活多选删除
      this.multiple = !selection.length
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.editForm.open = true;
      this.editForm.isModify = false;
      this.editForm.title = "新增服务器信息表";
    },
    // 删除按钮操作
    handleDelete: debounceWrapper(function (row) {
      const deleteIds = row ? row.serverId : this.ids.join(",");
      deleteByIds(deleteIds)
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () { });
    }),
    // 修改按钮操作
    handleUpdate: debounceWrapper(function (row) {
      this.reset();
      selectById(row.serverId).then(response => {
        this.editForm.data = response.data;
        this.editForm.open = true;
        this.editForm.isModify = true;
        this.editForm.title = "修改服务器数据源信息表";
      });
    }),
    // 提交按钮
    submitForm: debounceWrapper(function () {
      this.$refs["formRef"].validate(valid => {
        if (valid) {
          // 创建人
          delete this.editForm.data.createBy;
          // 创建时间
          delete this.editForm.data.createTime;
          // 更新人
          delete this.editForm.data.updateBy;
          // 更新时间
          delete this.editForm.data.updateTime;

          const ifUpdate = this.editForm.isModify;
          const promise = ifUpdate ? updateById(this.editForm.data) : insert(this.editForm.data);
          promise.then(response => {
            if (response.data === 1) {
              this.msgSuccess(ifUpdate ? '修改成功' : '新增成功');
              this.editForm.open = false;
              this.getList();
            }
          });
        }
      });
    }),
    // 取消按钮
    cancel() {
      this.editForm.open = false;
      this.reset();
    },
    //是否启用tag
    isEnableTag(isEnable) {
      //流程状态  Y启用 N禁用
      if(isEnable == 'Y') {
        return 'success'
      } else if(isEnable == 'N') {
        return 'danger'
      }
    },     
  },
}
</script>

<style lang="scss" scoped>
</style>